
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  type;

import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.sql.ResultSet;
import java.util.Hashtable;
import java.util.Vector;

import logger.PoolLogger;

import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import file.FileUtility;
import file.FileWriter;
import utility.Input;


public class Receiver 
{	
	//SearchRecord sr;
	RecordSearch ps;
	String RECEIVER="receiver";
	GetIdName gid;
	String EMAILID="Emailid";
	int index_pv=-1;
	int index_vt=-1;
	String SendMode;
	String mid;
	String PERSONCODE="personcode";
	String transcation_type;
	String TRASACTION_TYPE="transaction_type";
	PoolLogger pl;
	
    public Receiver(String mid,String transcation_type,String SendMode) 
     { 
    	 this.mid=mid;
    	// this.db =db; 
    	 gid =new  GetIdName();
    	 RECEIVER = gid.getId(RECEIVER);
    	 TRASACTION_TYPE = gid.getId(TRASACTION_TYPE);
    	 this.SendMode=SendMode;
    	 this.SendMode = gid.getId(SendMode);
    	 this.transcation_type =transcation_type;
    	// logger
 		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
    	 PERSONCODE = gid.getId(PERSONCODE);
     }
     
     public Vector<Object> findReceiver(String linet,String id)
 	 {
    		Vector<Object> RECEIVERnumber = new Vector<Object>();
    	try 
    	{
    	
			//RandomAccessFile raf =new RandomAccessFile(path,"r");  
			//FileUtility fu =new FileUtility(raf);
//			index_vt = fu.getColumnIndex("vt")-1;
//			index_pv = fu.getColumnIndex("pv")-1;
    		index_vt = Column.vt_index-1;
			index_pv = Column.pv_index-1;
			
			Hashtable<Object, Object>htable = new Hashtable<Object, Object>();
			htable.put("pid", RECEIVER);
			htable.put("mid",mid);
			/*sr = new SearchRecord(path);
			sr.fileOpen();
			String line[]  = sr.getRecordSet(htable);
			sr.fileClose();*/
			htable.put("td","null");
			ps = new RecordSearch();
			ps.setConditionMap(htable);
			String line[] = ps.getArrayFromResultSet();
			for(int i=0;i<line.length;i++)
			{
				String tempNumber;
				String [] Split = line[i].split("\t");
				if(Split[index_pv].equals("0"))
					tempNumber = getRECEIVER(Split[index_vt],linet,id);
				else
					tempNumber = getRECEIVER(gid.getItem(Split[index_pv]),linet,id);
				if(RECEIVERnumber.contains(tempNumber)==false && !tempNumber.equals("null"))
					RECEIVERnumber.add(tempNumber);
			}
			//raf.close();
			System.out.println("The mobile number :  " +RECEIVERnumber);
			pl.info("The mobile number :  " +RECEIVERnumber);
			pl.debug("The mobile number :  " +RECEIVERnumber);
			pl.warn("The mobile number :  " +RECEIVERnumber);
			pl.fatal("The mobile number :  " +RECEIVERnumber);
			pl.error("The mobile number :  " +RECEIVERnumber);
			pl.trace("The mobile number :  " +RECEIVERnumber);
			return RECEIVERnumber;
		} 
    	catch (Exception e) 
    	{
		}
    	return RECEIVERnumber;
 	}
     
     
     public String getRECEIVER(String reciver,String eventObject,String id)
 		{
    	 
    	 try 
 		{
 			String sub_reciver = reciver.substring(1);
 			long mobile_number = Long.parseLong(sub_reciver);
 			System.out.println("Mobile No is:"+mobile_number);
 			pl.info("Mobile No is:"+mobile_number);
			pl.debug("Mobile No is:"+mobile_number);
			pl.warn("Mobile No is:"+mobile_number);
			pl.fatal("Mobile No is:"+mobile_number);
			pl.error("Mobile No is:"+mobile_number);
			pl.trace("Mobile No is:"+mobile_number);
 			return reciver;
 		}
 		catch (NumberFormatException e) 
 		{
 			try 
 			{
				if(reciver.contains("@")) 
					return reciver;
				
				ResultSet rs=DBManager.getSelect("Select "+reciver+ " from transaction where id="+id);
				String property="";
				if(rs.next())
				{
					property = rs.getString(reciver);
				}
				
				//String path = Input.MASTERREPORTSPATH + name[index] + "//" + name[index] + "_mid" + Input.FILRFORMAT; 
				Hashtable<Object, Object> table=  new Hashtable<Object, Object>();
				table.put("pid", PERSONCODE);
				table.put("pv", property);
				table.put("td","null");
				ps = new RecordSearch();
				ps.setConditionMap(table);
				table.clear();
				String result[] =ps.getArrayFromResultSet();
				for(int k=0;k<result.length;k++)
				{
					String split[]=result[k].split("\t");
					table.put("pid",SendMode);
					table.put("mid", split[Column.mid_index-1]);
					table.put("td","null");
					ps = new RecordSearch();
					ps.setConditionMap(table);
					String line[] = ps.getArrayFromResultSet();
					for(int i=0;i<line.length;i++)
					{	
						String Split[]=line[i].split("\t");
						if(Split[index_pv].equals("0"))
							return Split[index_vt];
						else
							return gid.getItem(Split[index_pv]);
					}
				}
			}
 			catch (Exception e1) 
 			{
				e1.printStackTrace();
			}
 		
 		} 
 		return "null";
 	}
    public static void main(String[] args) 
    {
    	String eventobject="5616";
    	String path=Input.MASTERREPORTSPATH + eventobject + "//" +eventobject +"_mid" + Input.FILRFORMAT;;
    //	Receiver re = new Receiver(path,"68",db,"Mobileno");
    	//System.out.print(re.findReceiver("5634	2007-04-16 14:04:38	68	0	3970	0	26"));
	} 
}
